import 'package:flutter/material.dart';

enum SearchBarType { home, normal, homeLight }

class SearchBar extends StatefulWidget {
  final bool enabled;
  final bool? hideLeft;
  final bool? hideRight;
  final SearchBarType searchBarType;
  final String? hint;
  final String? defaultText;
  final EdgeInsets? inputMargin;
  final void Function()? leftButtonClick;
  final void Function()? rightButtonClick;
  final void Function()? speakClick;
  final void Function()? inputBoxClick;
  final ValueChanged<String>? onChanged;
  final Function(String)? onSubmit;
  final void Function()? onCancel;


  const SearchBar(
      {Key? key,
        this.enabled = true,
        this.hideLeft,
        this.hideRight,
        this.searchBarType = SearchBarType.normal,
        this.hint,
        this.defaultText,
        this.leftButtonClick,
        this.rightButtonClick,
        this.speakClick,
        this.inputBoxClick,
        this.inputMargin,
        this.onChanged, this.onSubmit, this.onCancel})
      : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  bool showClear = false;
  bool search = false;
  bool cancel = false;
  final TextEditingController _controller = TextEditingController();
  @override
  void initState() {
    if (widget.defaultText != null) {
      _controller.text = widget.defaultText!;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return widget.searchBarType == SearchBarType.normal
        ? _normalSearch()
        : _homeSearch();
  }

  _normalSearch() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        child: Row(
          children: <Widget>[

            Expanded(child: _inputBox()),
            showClear&&search?GestureDetector(
              child: Padding(
                padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                child: Text('搜索'),
              ),
              onTap: (){
                setState(() {
                  search =false;
                  cancel = true;
                });
                widget.onSubmit!(_controller.text);
              },
            ):cancel?GestureDetector(
              child: Padding(
                padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                child: Text('取消'),
              ),
              // onTap: widget.onCancl,
              onTap: (){
                setState(() {
                  _controller.text = '';
                  cancel = false;
                  showClear = false;
                });
                widget.onSubmit!('');
              },
            ):Container()
            // _tap(
            //     Container(
            //         child: widget.hideRight ?? true
            //             ? null
            //             : Padding(
            //                 padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
            //                 child: Text('搜索'),
            //               )),
            //     widget.onSubmit(_controller.text??'')),
          ],
        ),
      ),
    );
  }

  _inputBox() {
    Color inputBoxColor;
    if (widget.searchBarType == SearchBarType.home) {
      inputBoxColor = Color(0xfff3f3f3);
    } else {
      inputBoxColor = Color(0xfff3f3f3);
    }
    return Container(
      height: 36,
      margin: widget.inputMargin,
      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      decoration: BoxDecoration(
          color: inputBoxColor,

          borderRadius: BorderRadius.circular(
              widget.searchBarType == SearchBarType.normal ? 16 : 16)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Icon(Icons.search,
              size: 20,
              color: widget.searchBarType == SearchBarType.normal
                  ? Color(0xffa9a9a9)
                  : Color(0xffa9a9a9)),
          Expanded(
              child: widget.searchBarType == SearchBarType.normal
                  ? TextField(
                controller: _controller,
                onChanged: _onChanged,
                onSubmitted: _onSubmit,
//                      autofocus: true,
                enabled: widget.enabled,
                style: TextStyle(
                    fontSize: 18,
                    color: Colors.black,
                    fontWeight: FontWeight.w300),
                //输入文本样式
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.fromLTRB(5, 0, 5, 15),
                    border: InputBorder.none,
                    hintText: widget.hint ?? '',
                    hintStyle: TextStyle(fontSize: 15,color: Color(0xffb6b6b8))),
              )
                  : _tap(
                  Container(
                    child: Text(
                      widget.defaultText!,
                      style: TextStyle(fontSize: 13, color: Colors.grey),
                    ),
                  ),
                  widget.inputBoxClick)),
          showClear
              ? _tap(
              Icon(
                Icons.clear,
                size: 22,
                color: Colors.grey,
              ), () {
            setState(() {
              _controller.clear();
            });
            widget.onSubmit!('');
            _onChanged('');
          })
              : Container()
        ],
      ),
    );
  }

  _homeSearch() {
    return Padding(
      padding: const EdgeInsets.all(5.0),
      child: Container(
        child: Row(
          children: <Widget>[
//            _tap(
//                Container(
//                    child: Row(
//                  children: <Widget>[
//                    Text(
//                      '上海',
//                      style: TextStyle(color: _homeFontColor(), fontSize: 14),
//                    ),
//                    Icon(
//                      Icons.expand_more,
//                      color: _homeFontColor(),
//                      size: 22,
//                    )
//                  ],
//                )),
//                widget.leftButtonClick),
            Expanded(child: _inputBox()),

            _tap(
                Container(
                    padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                    child: Icon(
                      Icons.dehaze,
                      color: _homeFontColor(),
                      size: 26,
                    )),
                widget.rightButtonClick),
          ],
        ),
      ),
    );
  }

  _tap(Widget child,  callback) {
    return GestureDetector(
      onTap: () {
        if (callback != null) callback();
      },
      child: child,
    );
  }

  _onChanged(String text) {
    if (text.length > 0) {
      setState(() {
        showClear = true;
        search = true;
      });
    } else {
      setState(() {
        showClear = false;
        search = false;

      });
    }
    if (widget.onChanged != null) {
      widget.onChanged!(text);
    }
  }

  _onSubmit(String text) {
    if (text.length > 0) {
      setState(() {
        showClear = true;
      });
    } else {
      setState(() {
        showClear = false;
      });
    }
    if (widget.onSubmit != null) {
      widget.onSubmit!(text);
    }
  }

  _homeFontColor() {
    return widget.searchBarType == SearchBarType.homeLight
        ? Colors.black54
        : Colors.black54;
  }
}
